const { create, updateCountService, deleteCartService, addCartService } = require('../service/cart.service')
const errorType = require('../constant/errorType')
class CartController {
  async getCartData(ctx, next) {
    const { id } = ctx.user
    // 数据库查询
    const res = await create(id)
    ctx.body = {
      status: 200,
      data: res,
      msg: 'ok'
    }
    // 返回数据
  }
  async updateCartCount(ctx, next) {
    const { userId, goodsId, newCount } = ctx.request.body
    const res = await updateCountService(userId, goodsId, newCount)
    if (res) {
      ctx.body = {
        status: 200,
        msg: 'ok'
      }
    }
  }
  async deleteCart(ctx, next) {
    const { id } = ctx.request.body
    const res = await deleteCartService(id)
    if (res) {
      ctx.body = {
        status: 200,
        msg: 'ok'
      }
    }
  }
  async addCart(ctx, next) {
    const { goodsId, userId } = ctx.request.body
    const count = 1
    console.log('goodsId', goodsId);
    console.log('userId', userId);
    const res = await addCartService(goodsId, userId, count)
    ctx.body = {
      status: 200,
      data: res,
      msg: 'ok'
    }
  }
}

module.exports = new CartController()